import { createRoot } from 'react-dom/client';
import { ReactElement } from 'react';

export function renderToBody(element: ReactElement): () => void {
  const container = document.createElement('div');
  document.body.appendChild(container);
  const root = createRoot(container);
  root.render(element);

  return () => {
    root.unmount();
    if (container.parentNode) {
      container.parentNode.removeChild(container);
    }
  };
}
